package com.xmg.client.mgrsite.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.domain.Brand;
import com.xmg.shop.api.query.BrandQueryObject;
import com.xmg.shop.api.service.IBrandService;
import com.xmg.shop.api.service.ICatalogService;
import com.xmg.shop.api.util.AjaxResult;
import com.xmg.shop.api.util.UploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.util.Date;

@Controller
public class BrandController {
    @Reference
    private IBrandService brandService;
    @Reference
    private ICatalogService catalogService;
    @Value("${uploadPath}")
    private String uploadPath;

    @RequestMapping("/brand")
    public String brandPage(@ModelAttribute("qo") BrandQueryObject qo, Model model){
        model.addAttribute("pageResult",brandService.query(qo));
        model.addAttribute("catalog",catalogService.selectAll());
        return "brand";
    }

    @RequestMapping("/brand_save")
    @ResponseBody
    public AjaxResult save(Brand brand, Long[] catalogId){
        try {
            brandService.saveORupdate(brand,catalogId);
            return new AjaxResult(true,"");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        try {
            brandService.delete(id);
            return new AjaxResult(true,"");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult();
        }
    }

    @RequestMapping("/uploadImage")
    @ResponseBody
    public String uploadImage(MultipartFile image){
        return UploadUtil.upload(image,uploadPath);
    }
}
